package com.shishuo.cms.tag;

import static freemarker.template.ObjectWrapper.BEANS_WRAPPER;

import java.io.IOException;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shishuo.cms.entity.Admin;
import com.shishuo.cms.entity.vo.ArticleCommentVo;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.exception.FolderNotFoundException;
import com.shishuo.cms.service.ArticleCommentService;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

@Service
public class ArticleCommentPageTag implements TemplateDirectiveModel {
	@Autowired
	private ArticleCommentService articleCommentService;

	@Override
	public void execute(Environment env, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		// 获取页面的参数
		Integer articleId = Integer
				.parseInt(params.get("articleId").toString());
		Integer p = Integer.parseInt(params.get("p").toString());
		Integer rows = Integer.parseInt(params.get("rows").toString());
		// 获取文件的分页
		try {
			//PageVo<ArticleCommentVo> pageVo = new PageVo<ArticleCommentVo>(p);
			PageVo<ArticleCommentVo> pageVo = articleCommentService.getPagesListByArticleId(articleId,p,rows);
			env.setVariable("tag_article_comment_page", BEANS_WRAPPER.wrap(pageVo));
		} catch (Exception e) {
			env.setVariable("tag_article_comment_page", BEANS_WRAPPER.wrap(null));
		}
		body.render(env.getOut());
	}

}
